/* eslint-disable no-prototype-builtins */

import Vue from "vue";
import BasicBtn from "../baseUi/globalBtn/index.vue";
// eslint-disable-next-line no-unused-vars
let firstMenu = null;
export function loadComponent() {
  const requireComponent = require.context(
    "../components/satCpn",
    true,
    /[A-Z]\w+\.(vue)$/
  );
  requireComponent.keys().forEach((filename) => {
    const componentName = filename.replace(/^\.\//, "").replace(/\.\w+$/, "");
    const componentConfig = requireComponent(filename);
    Vue.component(componentName, componentConfig.default || componentConfig);
  });
}
export function loadGlobalCpn() {
  Vue.component("basic-btn", BasicBtn);
}

export function mapMenusToRoutes(menus) {
  //模拟拿到后台菜单表
  menus = [
    {
      id: 1,
      label: "卫星1",
      url: "/home/satIndex/1",
      routeName: "satIndex",
      children: [
        {
          id: 5,
          label: "传感器",
          url: "/home/satIndex/1",
          routeName: "sensor",
        },
      ],
    },
    {
      id: 2,
      label: "卫星2",
      url: "/home/satIndex/2",
      routeName: "satIndex",
      children: [
        {
          id: 6,
          label: "传感器",
          url: "/home/satIndex/5/sensor/6",
          routeName: "sensor",
        },
      ],
    },
    {
      id: 3,
      label: "火箭",
      url: "/home/socket/3",
      routeName: "socket",
    },
    {
      id: 4,
      label: "测站",
      url: "/home/station/4",
      routeName: "station",
    },
  ];
  console.log("mymenus", menus);
  let routes = []; //根据菜单筛选出的路由
  const allRoutes = []; //全部路由表
  const requireComponent = require.context(
    "../router/home",
    true,
    /[a-z]\w+\.(js)$/
  );
  requireComponent.keys().forEach((filename) => {
    const file = filename.replace(/^\.\//, "");
    const route = require("../router/home/" + file);
    allRoutes.push(route.default); //添加所有动态路由
  });
  console.log("allroutes", allRoutes);

  // routes = allRoutes.filter((route) => {
  //   if (route.children && route.children.length) {
  //     route.children = route.children.filter((routechild) => {
  //       for (const menu of menus) {
  //         if (menu.children && menu.children.length) {
  //           menu.children.forEach((menuChildItme) => {
  //             return menuChildItme.routeName === routechild.name;
  //           });
  //         } else {
  //           return false;
  //         }
  //       }
  //     });
  //     if (route.children && route.children.length) {
  //       return true;
  //     } else {
  //       return false;
  //     }
  //   } else {
  //     for (const menu of menus) {
  //       return menu.routeName === route.name;
  //     }
  //   }
  // });
  // 传入当前用户菜单找出匹配的路由
  // const _recurseGetRoute = (menus) => {
  //   for (const menu of menus) {
  //     if (menu.children && menu.children.length) {
  //       console.log(1234);
  //       _recurseGetRoute(menu.children);
  //     } else {
  //       const route = allRoutes.find((route) => {
  //         if (route.children && route.children.length) {
  //           route.children = route.children.filter((routeChildrenItem) => {
  //             return routeChildrenItem.name === menu.routeName;
  //           });
  //           if (route.children.length > 0) {
  //             return true;
  //           }
  //         } else {
  //           return menu.routeName === route.name;
  //         }
  //       });
  //       if (route) routes.push(route);
  //     }
  //   }
  // };

  // _recurseGetRoute(menus);
  console.log("finalyroutes", routes);
  return routes;
}
